package com.mocaris.android.gank.view.wanandroid

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.snackbar.Snackbar
import com.mocaris.android.gank.adapter.BannerAdapter
import com.mocaris.android.gank.R
import com.mocaris.android.gank.adapter.ArticleListAdapter
import com.mocaris.android.gank.adapter.BaseRecyclerAdapter
import com.mocaris.android.gank.data.LiveDataResouce
import com.mocaris.android.gank.model.Banner
import com.mocaris.android.gank.vm.AndroidMainViewModel
import kotlinx.android.synthetic.main.android_home_fragment.*

class AndroidHomeFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener, BaseRecyclerAdapter.OnItemClickListener {

    companion object {
        fun newInstance() = AndroidHomeFragment()
    }

    private lateinit var viewModel: AndroidMainViewModel
    private lateinit var bannerAdapter: BannerAdapter
    private lateinit var articleListAdapter: ArticleListAdapter

    private var articlePage = 0
    private var pageCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(AndroidMainViewModel::class.java)
        bannerAdapter = BannerAdapter(activity!!)
        articleListAdapter = ArticleListAdapter(activity!!)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.android_home_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        srl_home.setOnRefreshListener(this)
        viewModel.bannerData.observe(this, bannerObserver)
        vp_banner.adapter = bannerAdapter
        rv_article.adapter = articleListAdapter
        articleListAdapter.onItemClickListener = this
        rv_article.addOnScrollListener(onScrollListener)
        viewModel.getBanner()
        getArticle()
    }

    private val onScrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                if (!recyclerView.canScrollVertically(RecyclerView.SCROLL_INDICATOR_BOTTOM) && pageCount != articlePage) {
                    getArticle()
                }
            }
        }
    }

    override fun onItemClick(viewHolder: BaseRecyclerAdapter.ViewHolder, position: Int) {

    }


    private fun getArticle() {
        viewModel.getHomeArticle(articlePage).observe(this, Observer {
            if (srl_home?.isRefreshing == true) {
                srl_home.isRefreshing = false
            }
            if (it.result) {
                pageCount = it.data!!.second
                if (articlePage == 0) {
                    articleListAdapter.refreshData(it.data!!.first)
                } else {
                    articleListAdapter.appendData(it.data!!.first)
                }
                articlePage++
            } else {
                Snackbar.make(view!!, "文章获取失败，请重试", Snackbar.LENGTH_SHORT).show()
            }
        })
    }

    override fun onRefresh() {
        articlePage = 0
        getArticle()
    }


    private val bannerObserver = Observer<LiveDataResouce<List<Banner>>> {
        if (it.result) {
            bannerAdapter.bannerList = it.data!!
        } else {
            Snackbar.make(view!!, "banner获取失败，请重试", Snackbar.LENGTH_SHORT).show()
        }
    }

}
